package com.mark.service.impl;

import com.mark.entity.school.*;
import com.mark.mapper.*;
import com.mark.service.UTeacherService;
import com.mark.utils.StudentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * (UTeacher)表服务实现类
 *
 * @author Deng PengFei
 * @since 2021-10-27 11:17:56
 */
@Service
public class UTeacherServiceImpl implements UTeacherService {
    @Autowired
    private UTeacherMapper uTeacherMapper;

    @Autowired
    private UAccountMapper uAccountMapper;

    @Autowired
    private UTeacherGroupDetailMapper uTeacherGroupDetailMapper;

    @Autowired
    private USubjectMapper uSubjectMapper;

    @Autowired
    private UTeacherGroupMapper uTeacherGroupMapper;


    /**
     * 增加一个教师
     *
     * @param uTeacher 教师实例
     * @return 1001 成功 、  1002 教师已存在 、 1003 失败
     */
    @Transactional
    @Override
    public Integer addNewTea(UTeacher uTeacher, UTeacherGroup uTeacherGroup) {
        //查询教师是否存在
        UTeacher newTea = new UTeacher();
        newTea.setTeaNm(uTeacher.getTeaNm());
        List<UTeacher> teachers = this.uTeacherMapper.queryAll(newTea);
        if (teachers.size() > 0) {
            return 1002;
        }
        //生成教师登录账户名   当前日期 + 3位随机数
        SimpleDateFormat date = new SimpleDateFormat("yyyy");
        String str = date.format(new Date());
        //查询员工号是否已经存在
        int logNameSum, num;
        String logName;
        UAccount account = new UAccount();
        do {
            //1.3 生成3位随机数
            num = (new Random().nextInt(999 - 100 + 1) + 100);
            logName = str + num;
            account.setLoginName(logName);
            //查询教师登录账户号是否已经存在
            logNameSum = this.uAccountMapper.queryByCondition(account).size();
        } while (logNameSum != 0);
        //往账户表插入一条数据 获取账户自增  accId
        account.setLoginName(logName);
        account.setLoginPass(StudentUtil.getRandomPass());  // 设置生成的随机密码
        account.setRole(2);  // 设置角色为学生;
        this.uAccountMapper.insert(account);  // 获取插入数据的主键ID 获取插入数据的主键ID
        uTeacher.setAccId(account.getAccId());
        //往教师表插入一条数据 获取插入数据的主键ID
        uTeacher.setTeaFromDate(str);
        USubject uSubject = this.uSubjectMapper.getSubBySubId(uTeacher.getTeaMajorSubId());
        uTeacher.setTeaMajorSubNm(uSubject.getSubNm());
        if (uTeacher.getTeaSubId2() != null) {  // 判断是否教第二学科
            uSubject = this.uSubjectMapper.getSubBySubId(uTeacher.getTeaSubId2());
            uTeacher.setTeaSubNm2(uSubject.getSubNm());
        }
        if (uTeacher.getTeaSubId3() != null) {  // 判断是否教第三学科
            uSubject = this.uSubjectMapper.getSubBySubId(uTeacher.getTeaSubId3());
            uTeacher.setTeaSubNm3(uSubject.getSubNm());
        }
        this.uTeacherMapper.insert(uTeacher);
        //如果教师分组不为空 则需要往教师 与 教师组 关系表 插入一条数据
        if (uTeacherGroup != null) {
            //查询分组信息
            uTeacherGroup = this.uTeacherGroupMapper.getTeaGroupByGroId(uTeacherGroup.getGroId());
            UTeacherGroupDetail uTeacherGroupDetail = new UTeacherGroupDetail();
            uTeacherGroupDetail.setGroId(uTeacherGroup.getGroId());
            uTeacherGroupDetail.setGroNm(uTeacherGroup.getGroNm());
            uTeacherGroupDetail.setTeaId(uTeacher.getTeaId());
            uTeacherGroupDetail.setTeaNm(uTeacher.getTeaNm());
            this.uTeacherGroupDetailMapper.insert(uTeacherGroupDetail);
        }
        return 1001;
    }

    /**
     * 通过班级编号来获取教师信息
     *
     * @param claId 班级编号
     * @return 对象列表
     */
    @Override
    public List<UTeacher> getTeacherListByClaId(Integer claId) {
        // 教师与学科关联  学科与班级关联


        return null;
    }

    /**
     * 批量导入教师信息
     *
     * @param exclList 读取到的excl信息
     */
    @Override
    public void importTeaList(ArrayList<ArrayList<String>> exclList) {
        //解析教师信息
        ArrayList<String> title = exclList.get(0);   // 获取到标题行
        List<UTeacher> teacherList = new ArrayList<>();
        List<UTeacherGroup> teacherGroupList = new ArrayList<>();
        for (int i = 1; i < exclList.size(); i++) {
            ArrayList<String> teaInfo = exclList.get(i); // 获取内容行
            UTeacher uTeacher = new UTeacher();
            UTeacherGroup uTeacherGroup = new UTeacherGroup();
            for (int j = 0; j < teaInfo.size(); j++) {
                switch (title.get(j).trim()) {
                    case "姓名":
                        uTeacher.setTeaNm(teaInfo.get(j).trim());
                        break;
                    case "手机号":
                        uTeacher.setTeaMobile(teaInfo.get(j).trim());
                        break;
                    case "性别":
                        uTeacher.setTeaSex(Integer.parseInt(teaInfo.get(j).trim()));
                        break;
                    case "任教科目1Id":
                        uTeacher.setTeaMajorSubId(Integer.parseInt(teaInfo.get(j).trim()));
                        break;
                    case "是否任教科目1负责人":
                        uTeacher.setIsMajorSubManager(Integer.parseInt(teaInfo.get(j).trim()));
                        break;
                    case "任教科目2Id":
                        if ("".equals(teaInfo.get(j).trim()) || null == teaInfo.get(j)) {
                            uTeacher.setTeaSubId2(null);
                        } else {
                            uTeacher.setTeaSubId2(Integer.parseInt(teaInfo.get(j).trim()));
                        }
                        break;
                    case "是否任教科目2负责人":
                        if ("".equals(teaInfo.get(j).trim()) || null == teaInfo.get(j)) {
                            uTeacher.setIsSubManager2(null);
                        } else {
                            uTeacher.setIsSubManager2(Integer.parseInt(teaInfo.get(j).trim()));
                        }
                        break;
                    case "任教科目3Id":
                        if ("".equals(teaInfo.get(j).trim()) || null == teaInfo.get(j)) {
                            uTeacher.setTeaSubId3(null);
                        } else {
                            uTeacher.setTeaSubId3(Integer.parseInt(teaInfo.get(j).trim()));
                        }
                        break;
                    case "是否任教科目3负责人":
                        if ("".equals(teaInfo.get(j).trim()) || null == teaInfo.get(j)) {
                            uTeacher.setIsSubManager3(null);
                        } else {
                            uTeacher.setIsSubManager3(Integer.parseInt(teaInfo.get(j).trim()));
                        }
                        break;
                    case "分组Id":
                        if ("".equals(teaInfo.get(j).trim()) || null == teaInfo.get(j)) {
                            uTeacherGroup.setGroId(null);
                        } else {
                            uTeacherGroup.setGroId(Integer.parseInt(teaInfo.get(j).trim()));
                        }
                        break;
                    default:
                        break;
                }
            }
            teacherList.add(uTeacher);
            teacherGroupList.add(uTeacherGroup);
        }
        for (int i = 0; i < teacherList.size(); i++) {
            this.addNewTea(teacherList.get(i), teacherGroupList.get(i));
        }
    }

    /**
     * 根据分组Id获取教师组的信息
     *
     * @param groId 分组Id
     * @return 对象列表
     */
    @Override
    public List<UTeacher> getTeaListByGroId(Integer groId) {
        return this.uTeacherMapper.getTeaListByGroId(groId);
    }

    /**
     * 获取教师列表
     *
     * @return 对象列表
     */
    @Override
    public List<UTeacher> getTeaList(String query) {
        return this.uTeacherMapper.getTeaList(query);
    }


    /**
     * 获取所有的教师信息
     *
     * @return 对象列表
     */
    @Transactional
    @Override
    public Boolean delTeaByTeaId(Integer teaId) {
        //删除教师组与教师分组对应的
        return null;
    }
}